﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using rock.Model;
using rock.DAL;
using rock.UI.common;

namespace rock.UI.houseinfo
{
    public partial class FormHouseDongUpdate : Form
    {
        ModelHOUSE_DONG modelHOUSE_DONG = new ModelHOUSE_DONG();
        public ModelHOUSE_DONG _ModelHOUSE_DONG
        {
            get
            {
                return modelHOUSE_DONG;
            }
            set
            {
                modelHOUSE_DONG = value;
            }
        }


        DalHOUSE_DONG dalHOUSE_DONG = new DalHOUSE_DONG();
        DalHOUSE_XIAOQU dalHOUSE_XIAOQU = new DalHOUSE_XIAOQU();
        DalCFG_CODE dalCFG_CODE = new DalCFG_CODE();
        public FormHouseDongUpdate()
        {
            InitializeComponent();
            this.AcceptButton = this.button1;
            this.CancelButton = this.button2;
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.WindowState = FormWindowState.Normal;
            this.StartPosition = FormStartPosition.CenterScreen;
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
            this.Icon = global::rock.Properties.Resources.house;
        }

        private void FormHouseDongUpdate_Load(object sender, EventArgs e)
        {
            //this.textBox1Name.Text = this._ModelHOUSE_XIAOQU.NAME;
            this.textBox3AllFloorStart.Text = this._ModelHOUSE_DONG.ALL_FLOOR_START+"";
            this.textBox3AllFloorEnd.Text = this._ModelHOUSE_DONG.ALL_FLOOR_END + "";
            this.textBox5Name.Text = this._ModelHOUSE_DONG.NAME;
            this.textBox4Address.Text = this._ModelHOUSE_DONG.ADDRESS;
            this.textBox6Note.Text = this._ModelHOUSE_DONG.NOTE;

            DataSet houseXiaoquSet = this.dalHOUSE_XIAOQU.GetList("");
            this.comboBox1.DisplayMember = "NAME";
            this.comboBox1.ValueMember = "SEQID";
            this.comboBox1.DataSource = houseXiaoquSet.Tables[0];
            if (this._ModelHOUSE_DONG.SEQID != 0)
            {
                this.comboBox1.SelectedValue = this._ModelHOUSE_DONG.HOUSE_XIAOQU_SEQID + "";
            }
            else {
                this.comboBox1.SelectedIndex = 0;
                //this.comboBox2.SelectedIndex = 0;
                
            }

           


            DataSet cfgCodeSet = this.dalCFG_CODE.GetList("TYPE = '楼宇类型'");
            this.comboBox2.DisplayMember = "CODE_VALUE";
            this.comboBox2.ValueMember = "CODE_VALUE";
            this.comboBox2.DataSource = cfgCodeSet.Tables[0];
            if (this._ModelHOUSE_DONG.TYPE != null && !this._ModelHOUSE_DONG.TYPE.Equals("")) {
                this.comboBox2.SelectedValue = this._ModelHOUSE_DONG.TYPE;
            }
            

           

            this.radioButton1.Checked = this._ModelHOUSE_DONG.HAS_DIANTI == 1 ? true : false;
            this.radioButton2.Checked = this._ModelHOUSE_DONG.HAS_DIANTI == 1 ? false : true;
        }
        private Boolean check()
        {
            try
            {
                StringUtils.checkText(this.textBox5Name.Text, 2, 30, "名称");
                StringUtils.checkIsAllInt(this.textBox3AllFloorStart.Text,   "开始楼层");
                StringUtils.checkIsAllInt(this.textBox3AllFloorEnd.Text, "结束楼层");
                int start = int.Parse( this.textBox3AllFloorStart.Text);
                int stop = int.Parse(this.textBox3AllFloorEnd.Text);
                if (stop < start)
                {
                    throw new Exception("结束楼层要大于或等于开始楼层.");
                }

                if (stop == start && start == 0)
                {
                    throw new Exception("结束楼层以及开始楼层不能全部为零.");

                }
                StringUtils.checkText(this.textBox4Address.Text, 0, 50, "地址");
                StringUtils.checkText(this.textBox6Note.Text, 0, 300, "备注信息");

            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message, "提示");
                return false;
            }
            return true;
        }


        private void button1_Click(object sender, EventArgs e)
        {
            if (!check())
            {
                return;
            }
            this._ModelHOUSE_DONG.NAME = this.textBox5Name.Text;
            this._ModelHOUSE_DONG.ALL_FLOOR_START = Convert.ToInt32(this.textBox3AllFloorStart.Text);
            this._ModelHOUSE_DONG.ALL_FLOOR_END = Convert.ToInt32(this.textBox3AllFloorEnd.Text);
            this._ModelHOUSE_DONG.ADDRESS = this.textBox4Address.Text;
            this._ModelHOUSE_DONG.NOTE = this.textBox6Note.Text;
            this._ModelHOUSE_DONG.HAS_DIANTI = this.radioButton1.Checked ? 1 : 0;
            this._ModelHOUSE_DONG.HOUSE_XIAOQU_SEQID = Convert.ToInt32(this.comboBox1.SelectedValue);
            this._ModelHOUSE_DONG.TYPE = (String)this.comboBox2.SelectedValue;



            if (dalHOUSE_DONG.Exists(this._ModelHOUSE_DONG.SEQID))
            {
                dalHOUSE_DONG.Update(this._ModelHOUSE_DONG);
            }
            else
            {
                dalHOUSE_DONG.Add(this._ModelHOUSE_DONG);
            }
            //触发代理
            oneRefreshParentData();
            this.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        //代理
        public delegate void RefreshParentData();
        public RefreshParentData oneRefreshParentData;

    }
}
